﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TravelManagement
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Page.User.Identity.Name))
            {
                btnSave.Visible = false;
                ddlMyMaps.Visible = false;
                lblSaveMap.Visible = false;
                lblMyMaps.Visible = false;
            }
            else
            {
                //To prevent same set of rows from adding again and agan
                if (!Page.IsPostBack)
                {
                    
                    RefreshUsersMaps();
                    
                }
            }

        }

        private void RefreshUsersMaps()
        {
            ddlMyMaps.Items.Clear();
            List<ListItem> items = GetMapsForCurrentUser();

            ddlMyMaps.Items.AddRange(items.ToArray());
        }

        private List<ListItem> GetMapsForCurrentUser()
        {
            using (TSPGMapsDBContainer entities = new TSPGMapsDBContainer())
            {
                var maps = from map in entities.Maps
                           orderby map.MapName
                           where map.User.UserName == Page.User.Identity.Name
                           select map;

                List<ListItem> items = new List<ListItem>();
                items.Add(new ListItem() { Text = "Please select an item", Value = String.Empty });
                foreach (var map in maps)
                {
                    items.Add(new ListItem() { Text = map.MapName, Value = map.MapLocation });
                }

                return items;

            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                string mapValue = hdnSaveDataField.Value;
                string userName = Page.User.Identity.Name;
                string mapName = hdnSaveName.Value;

                if (String.IsNullOrEmpty(mapName) || String.IsNullOrEmpty(userName) || String.IsNullOrEmpty(mapValue))
                {
                    return;
                }

                using (TSPGMapsDBContainer entities = new TSPGMapsDBContainer())
                {
                    Guid mapId = Guid.NewGuid();
                    Map map = new Map();
                    map.MapId = mapId;
                    map.MapLocation = mapValue;
                    map.MapName = mapName;
                    map.MapOwner = userName;

                    entities.AddToMaps(map);
                    entities.SaveChanges();
                    RefreshUsersMaps();
                    ddlMyMaps.Items.FindByText(mapName).Selected = true;
                }
            }
            catch (Exception ex)
            {

            }

        }

        private User GetUser()
        {
            User user = null;
            using (TSPGMapsDBContainer entities = new TSPGMapsDBContainer())
            {
                user = (from userRow in entities.Users
                        where userRow.UserName == Page.User.Identity.Name
                        select userRow).SingleOrDefault();
                return user;
            }
        }
    }
}
